package com.hxs.hxsserver.controller.user;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hxs.hxsserver.controller.BaseController;
import com.hxs.hxsserver.domain.dto.IdDTO;
import com.hxs.hxsserver.domain.dto.JsonResult;
import com.hxs.hxsserver.domain.dto.user.ChangePasswordDTO;
import com.hxs.hxsserver.domain.dto.user.QueryUserListDTO;
import com.hxs.hxsserver.domain.dto.user.UpdateUserDTO;
import com.hxs.hxsserver.domain.entity.user.User;
import com.hxs.hxsserver.domain.vo.UserVO;
import com.hxs.hxsserver.service.UserService;
import com.hxs.hxsserver.utils.DozerUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("user")
@Api(value = "用户模块接口")
public class UserController extends BaseController {

    @Resource
    private UserService userService;
    @Resource
    private DozerUtil dozerUtil;


    @ApiOperation(value = "获取用户信息", response = UserVO.class)
    @GetMapping("/userInfo")
    public JsonResult<UserVO> userInfo(Integer id) {
        UserVO vo = userService.userInfo(id);
        return JsonResult.success(vo);
    }
    @ApiOperation(value = "获取用户信息(根据token)", response = UserVO.class)
    @GetMapping("/userInfoByToken")
    public JsonResult<UserVO> userInfoByToken() {
        return JsonResult.success( dozerUtil.map(getUser(), UserVO.class));
    }


    @ApiOperation(value = "更新密码", response = Boolean.class)
    @PostMapping("/changePassword")
    public JsonResult<Boolean> changePassword(@RequestBody @Validated ChangePasswordDTO dto) {
        return JsonResult.success(userService.changePassword(dto,getUserId()));
    }

    @ApiOperation(value = "更新用户信息", response = Boolean.class)
    @PostMapping("/updateUser")
    public JsonResult<Boolean> updateUser(@RequestBody @Validated UpdateUserDTO dto) {
        return JsonResult.success(userService.updateUser(dto,dto.getId()));
    }

    @ApiOperation(value = "新增用户信息", response = Boolean.class)
    @PostMapping("/insertUser")
    public JsonResult<Boolean> insertUser(@RequestBody @Validated UpdateUserDTO dto) {
        return JsonResult.success(userService.insertUser(dto,getUser()));
    }

    @GetMapping("/getUserList")
    @ApiOperation(value = "获取用户列表")
    public JsonResult<IPage<UserVO>> getUserList(@ApiParam(value = "查询参数") @Validated QueryUserListDTO queryUserListDTO){
        return JsonResult.success(userService.getUserList(queryUserListDTO,getUser()));
    }

    @GetMapping("/getUserListByAlarm")
    @ApiOperation(value = "获取报警人用户列表")
    public JsonResult<List<UserVO>> getUserListByAlarm(){
        return JsonResult.success(userService.getUserListByAlarm(getUser()));
    }

    @PostMapping("/deleteUserById")
    @ApiOperation(value = "删除用户")
    public JsonResult<Boolean> deleteUserById(@RequestBody @Validated IdDTO idDTO){
        return JsonResult.success(userService.deleteUserById(idDTO,getUser()));
    }
}
